package com.heny.demo.utils;

import org.geotools.geometry.jts.JTS;
import org.geotools.referencing.CRS;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Point;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;

public class CoordinateConverter {

    private static final GeometryFactory geometryFactory = new GeometryFactory();

    public static Point convertFromCGCS2000ToWGS84(double x, double y) {
        try {
            // 源坐标系：CGCS2000 / 3-degree Gauss-Kruger CM 114E
            CoordinateReferenceSystem sourceCRS = CRS.decode("EPSG:4547"); // EPSG:4547 对应 CGCS2000 / 3-degree Gauss-Kruger zone 38 (CM 114E)

            // 目标坐标系：WGS84
            CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:4326");

            MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS, true);

            // 构建源点
            Point sourcePoint = geometryFactory.createPoint(new Coordinate(x, y));
            Point transformedPoint = (Point) JTS.transform(sourcePoint, transform);

            return transformedPoint;
        } catch (Exception e) {
            throw new RuntimeException("坐标转换失败", e);
        }
    }
}